/* author: soso 
 * date: 2010-12-20
 * this lib is used for rank.sesamr.com to genarate table dynamicly and support some animation effect of paging
 */
var currentPage = 1;
var currentUser = "0";
var rows = [];
var flag = true;

	function init_tips(ele){
		$(ele).find('a').poshytip({
			bgImageFrameSize: 3,
			content: function(){
				uname = $(this).attr("rel");
				html = '<ul class="tips">'
				html += '<li><a href="http://www.delicious.com/' + uname + '" > ' +uname +' in delicious</a></li>';
				html += '<li><a href="http://rank.sesamr.com/leader/' + uname + '" >' +uname +'\'s ranking details</a></li>';
				html += '</ul>';
				return html;		
			},
			alignTo: 'target',
			alignX: 'inner-left',
			offsetX: 40,
			offsetY: 0
		});

		$("#dtips").poshytip({
			bgImageFrameSize: 3,
			content: function(){
				uname = $(this).attr("rel");
				html = '<ul class="tips">'
				html += '<li><a href="http://www.delicious.com/' + uname + '" > ' +uname +' in delicious</a></li>';
				html += '</ul>';
				return html;		
			},
			alignTo: 'target',
			alignX: 'inner-left',
			offsetX: 0,
			offsetY: 5
		});
	}


	function loading(){
		if(flag){
			$("#loading").css({"display" : "none"});
		}else{
			$("#loading").css({"display" : "block"});
		}
	}

	function goto_user(){
		if(!flag) return false;
		flag = false;
		loading();
		currentUser = $('#ranker').val();

		url = "/get/name/" + currentUser;
		$.get(url ,function(data ,textStatus){
			var s = parseInt(data);
			if(!s){
				$("#error").fadeIn("slow").html("sorry,we can't find this user.").fadeOut(4000);
			}else{
				url = "/get/page/" + s + "/" + currentUser;
				$.get(url ,function(data , textStatus){
					flag = true;
					loading();
					id = currentPage;
					$("#table_container").append(data);
					if ($.browser.version == "7.0" || $.browser.version == "6.0"){
						$("#table" + id).remove();
					}else{
						$("#table" + id).animate({marginTop:"-=418"}, 1000 ,function(){
						$("#mytable").css({"top" : 0});
						$("#table" + id).remove();
						});			
					}
					init_tips("#table_container");
					currentPage = s;
				});

			}
		});
	}
	

	function prev(){
		if(currentPage < 2 || !flag){
			return false;
		}

		flag = false;
		loading();
		currentPage--;
		url = "/get/page/" + currentPage + "/" + currentUser;
		$.get(url ,function(data , textStatus){
				flag = true;
				loading();
				id = currentPage + 1;
				$("#table_container").prepend(data);
				if ($.browser.version == "7.0" || $.browser.version == "6.0"){
					$("#table" + id).remove();
				}else{
					$("#table" + currentPage).css({"marginTop" : -418});
					$("#table" + currentPage).animate({marginTop:"+=418"}, 1000 ,function(){
						$("#table" + id).remove();
					});	
				}
				init_tips("#table_container");
		});
	}

	function next(){
		if(!flag) return false;
		flag = false;
		loading();
		currentPage++;
		url = "/get/page/" + currentPage + "/" + currentUser;
		$.get(url ,function(data , textStatus){
				flag = true;
				id = currentPage - 1;
				loading();
				//rows[currentPage] = $("#mytable tr");
				$("#table_container").append(data);
				if ($.browser.version == "7.0" || $.browser.version == "6.0"){
					$("#table" + id).remove();
				}else{
					$("#table" + id).animate({marginTop:"-=418"}, 1000 ,function(){
					$("#mytable").css({"top" : 0});
					$("#table" + id).remove();
					});		
				}
				init_tips("#table_container");

		});
	}






var EasyTable = function(options){
	this.options = {
		"ele" : null,
		"speed" : 5000
	};


	this.options = $.extend(this.options , options);
	this.table = $(this.options.ele);
	this.rows = $(this.options.ele + " tr");
}


EasyTable.prototype = {

	show : function(){
		this.rows.remove();
		this.table.append(data);
	},

	insertRow: function(direction,rowElement){
		this.rows.remove();
		if(direction=="after"){
			this.table.append(rowElement);
		}else{
			this.table.prepend(rowElement);
		}

		this.table.animate({
			marginTop : '-=418'
		},this.options.speed,this.removeRow());
	},

	removeRow : function(rowCount){
		this.rows.remove();
	}
}
